// pages/character/list/list.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist:[],
    inputShowed: false,
    inputVal: "",
    testData: [{
      nameZh: '李有志1',
      gender: '女',
      raceName: '半兽人',
      classesName: '战士',
      level: 12,
      campaign: '斯大林格勒保卫战'
    }, {
      nameZh: '李有志2',
      gender: '男',
      raceName: '半精灵',
      classesName: '法师',
      level: 23,
      campaign: '诺曼底登陆战'
    }, {
      nameZh: '李有志3',
      gender: '女',
      raceName: '人类',
      classesName: '游侠',
      level: 34,
      campaign: '敦刻尔克大撤退'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCharacterList()
    wx.setNavigationBarTitle({
      title: '角色列表',
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getCharacterList: function () {
    let openid = app.globalData.openid
    console.log(openid)
    wx.request({
      url: app.globalData.domain + '/assistant/character/list/' + openid,
      success: res => {
        console.log(res.data)
        this.setData({
          datalist: res.data
        })
      }
    })
  },

  getCharacterDetail: function (e) {
    console.log(e.currentTarget.dataset.name)
    wx.navigateTo({
      url: '../get/get?name=' + e.currentTarget.dataset.name,
    })
  },

  createCharacter: function () {
    wx.navigateTo({
      url: '../create/create',
    })
  },

  searchCharacter: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    wx.request({
      url: app.globalData.domain + '/assistant/character/search/' + e.detail.value,
      success: res => {
        console.log(res.data)
        this.setData({
          datalist: res.data
        })
      }
    })
  },
  
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
    this.getCharacterList()
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  }
})